package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.DelegatingServerHttpResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;

import javax.xml.ws.Holder;
import java.util.Date;
import java.util.List;
import java.util.logging.Handler;

@Service
@Transactional
public class CartServiceImpl implements CartService {
    //加入购物车
    @Autowired
    private CartMapper cartMapper;

    @Override
    public Cart addCart(Cart cart) {
        //先查询购物车中是否有数据 通过用户id和菜品或套餐id
        LambdaQueryWrapper<Cart> qq = new LambdaQueryWrapper<>();
        //获取用户id
        qq.eq(Cart::getUserId, UserHolder.get().getId());
        //获取套餐id
        qq.eq(cart.getSetmealId() != null, Cart::getSetmealId, cart.getSetmealId());
        //获取菜品id
        qq.eq(cart.getDishId() != null, Cart::getDishId, cart.getDishId());
        //调用mapper查询
        Cart cart1 = cartMapper.selectOne(qq);
        //判断数据库是否有改套餐或菜品 没有新增 有数量+1
        if (cart1 == null) {
            //补全信息
            cart.setNumber(1);//数量为1
            cart.setUserId(UserHolder.get().getId()); // 补全用户id
            cart.setCreateTime(new Date());//手动补充时间
            cartMapper.insert(cart);//放入数据库
            cart1 = cart;///=====================================
        } else {
            cart1.setNumber(cart1.getNumber() + 1);
            cartMapper.updateById(cart1);
        }
        System.out.println("---------------------"+cart1);
        return cart1;
    }

    //查看购物车
    @Override
    public List<Cart> look() {
        //构建条件  select * from shopping_cart where user_id = ?
        LambdaQueryWrapper<Cart> ll = new LambdaQueryWrapper<>();
        ll.eq(Cart::getUserId, UserHolder.get().getId());// 从线程中获取登录用户id
        List<Cart> cartList = cartMapper.selectList(ll);
        return cartList;

    }

    @Override
    public Cart removeCart(Cart cart) {
        //先查询购物车中是否有数据 通过用户id和菜品或套餐id
        LambdaQueryWrapper<Cart> qq = new LambdaQueryWrapper<>();
        //获取用户id
        qq.eq(Cart::getUserId, UserHolder.get().getId());
        //获取套餐id
        qq.eq(cart.getSetmealId() != null, Cart::getSetmealId, cart.getSetmealId());
        //获取菜品id
        qq.eq(cart.getDishId() != null, Cart::getDishId, cart.getDishId());
        //调用mapper查询
        Cart cart1 = cartMapper.selectOne(qq);
        //判断数据库是否有改套餐或菜品 没有新增 有数量+1
        if (cart1.getNumber()>1){
            cart1.setNumber(cart1.getNumber() -1);
            cartMapper.updateById(cart1);
        }else  {
            cartMapper.deleteById(cart1);
        }

        return cart1;
    }
    //清空购物车
    @Override
    public void deleteAll() {
        //构建条件
        LambdaQueryWrapper<Cart> lq=new LambdaQueryWrapper<>();
        lq.eq(Cart::getUserId, UserHolder.get().getId());
        // 2.执行删除
        cartMapper.delete(lq);
    }

}
